﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.IO;
using System.Data.Odbc;
using System.Collections;
using System.Data.SqlClient;

namespace Profari_Heldur_Utan_Um_Prof
{
    public partial class BloodPressureForm : Form
    {

        private static SqlConnection con = new SqlConnection("Data Source=orninn.com;Initial Catalog=Notendaprofanir;Persist Security Info=True;User ID=SonjaNew;Password=Léttjógúrt ");
        private static DataTable dt = new DataTable();
        private static DataSet ds = new DataSet();
        SqlDataReader r = null;

        //Global variables
        //int numMeasurements;
        //int numMeasurementsSuccessful;
        //int numMeasurementsFailed;
        //int numCommentMarker;
        //private string visitID;
        private XmlDocument lastMeasurement = new XmlDocument();
       // private OdbcConnection MyConnection;
  

    //    private XmlDocument doc;
    //    private XmlElement root;

      //  public string PATH = @"dinamap.xml";
        

        public BloodPressureForm()
        {
            this.StartPosition = FormStartPosition.CenterScreen;
            InitializeComponent();

            try
            {
                lastMeasurement.Load("dinamap.xml");
            }
            catch (Exception ex)
            {
                MessageBox.Show("Could not load reference XML file: " +ex.ToString());
            }
        }

        private void button6_Click(object sender, EventArgs e)
        {
            /*lastMeasurement = BloodPressureTool.Dina_GetStateOn();
            doc = new XmlDocument();
            doc.Load(PATH);
            root = doc.DocumentElement;
            textBox1.Text = root.GetElementsByTagName("Systolic_blood_pressure_Value")[0].InnerText;
            textBox2.Text = root.GetElementsByTagName("Diastolic_blood_pressure_Value")[0].InnerText;
            textBox5.Text = root.GetElementsByTagName("Pulse_Value")[0].InnerText;*/

        }


        private void button2_Click(object sender, EventArgs e)
        {
            int interval = 30000;
            timer1.Enabled = true;
            timer1.Interval = interval;
            timer1.Start();
           /* var s = "";
            Hashtable r = responseToHash(BloodPressureTool.Dina_GetStateOn());
            MessageBox.Show(r.Count.ToString());
            foreach(string k in r.Keys) {
                s += String.Format("{0}: {1}", k, r[k]);
            }

            MessageBox.Show(s.Equals("Pulse_Value").ToString());
            MessageBox.Show(s);

            if (r.Count != 0)
            {                
                
                textBox1.Text = r["Systolic_blood_pressure_Value"].ToString();
                textBox2.Text = r["Diastolic_blood_pressure_Value"].ToString();
                textBox5.Text = r["Pulse_Value"].ToString();

             //   insertIntoBloodPressure();
                
            }*/
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            //XmlDocument currentMeasurement = new XmlDocument();

            var s = "";
            Hashtable r = responseToHash(BloodPressureTool.Dina_GetStateOn());
         //   MessageBox.Show(r.Count.ToString());
            foreach (string k in r.Keys)
            {
                s += String.Format("{0}: {1}", k, r[k]);
            }

           // MessageBox.Show(s.Equals("Pulse_Value").ToString());
           // MessageBox.Show(s);

            if (r.Count != 0)
            {

                textBox1.Text = r["Systolic_blood_pressure_Value"].ToString();
                textBox2.Text = r["Diastolic_blood_pressure_Value"].ToString();
                textBox5.Text = r["Pulse_Value"].ToString();

                insertIntoBloodPressure();

            }
        }

        private void insertIntoBloodPressure()
        {
            try
	        {
		        con.Open();
		        SqlCommand cmd = new SqlCommand("INSERT INTO BloodPressure(diastolic, systolic, pressure) VALUES("
			        + textBox2.Text + " ," + textBox1.Text + " ," + textBox5.Text + ")", con);
		        cmd.ExecuteNonQuery();
	        }
	        catch (SqlException ex)
	        {
		        Console.WriteLine("Error: {0}", ex.ToString());
	        }
	        finally
	        {
		        if (con != null)
		        {

			        con.Close();
		        }
	        }

        }

        // Build hash from XML data
        private Hashtable responseToHash(XmlDocument doc)
        {
            Hashtable h = new Hashtable();

            XmlNodeList doc_results = doc.GetElementsByTagName("Result");

            foreach (XmlNode pnode in doc_results)
            {
                foreach (XmlNode cnode in pnode.ChildNodes)
                {
                    if (cnode.Name == "Units")
                    {
                        h.Add(pnode.Attributes["name"].InnerText + "_" + cnode.Name, cnode.Attributes["name"].InnerText);
                    }
                    else if (cnode.Name == "Time_stamp")
                    {
                        DateTime d = new DateTime(
                            Convert.ToInt32(cnode.Attributes["year"].InnerText),
                            Convert.ToInt32(cnode.Attributes["month"].InnerText),
                            Convert.ToInt32(cnode.Attributes["day"].InnerText),
                            Convert.ToInt32(cnode.Attributes["hour"].InnerText),
                            Convert.ToInt32(cnode.Attributes["minute"].InnerText),
                            Convert.ToInt32(cnode.Attributes["second"].InnerText));

                        h.Add(pnode.Attributes["name"].InnerText + "_" + cnode.Name, d);
                    }
                    else
                        h.Add(pnode.Attributes["name"].InnerText + "_" + cnode.Name, cnode.InnerText);
                }
            }

            return h;
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            string szHour;
            szHour = DateTime.Now.ToString("h:mm:ss");
            lblTime.ForeColor = Color.Black;
            lblTime.Text = szHour;
        }

        private void BloodPressureForm_Load(object sender, EventArgs e)
        {
            timer2.Start();
        }


    }
}
